// pages/company/company.js
const app = getApp()
Page({
  data: {
   region: ['省，市，区'],
    checked: false,
    company:'',
    name:'',
    tel:'',
    num:'',
    email:'',
    model:false,
  },

  onLoad: function (options) {
    this.setData({
      userlogin: wx.getStorageSync('token'),
      tellogin: wx.getStorageSync('tel'),  
    })
  },

  onShow: function () {

  },
  telChange(e) {  //手机号码
    this.data.tel = e.detail.value;
  },
  companyChange(e) {  //企业名字
    console.log(e.detail.value)
    this.data.company = e.detail.value;
  },
  nameChange(e) {  //数量
    this.data.num = e.detail.value;
  },
 nameChange(e) {  //对接人
    this.data.name = e.detail.value;
  },
  emailChange(e) {  //邮箱
    this.data.email = e.detail.value;
  },
  bindtap1: function (e) { //协议选中
    this.setData({
      checked: !this.data.checked
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let region = e.detail.value;
    if (e.detail.value) {
      this.setData({
        region: region
      })
      console.log(this.data.region)
    }
  },
  numChange: function (e) { //协议选中
    this.data.num = e.detail.value;
  },
  detail: function (e) { //查看说明协议
    wx.navigateTo({
      url: '/pages/persondz/detailexplain/detailexpalin?type=company'　　// 协议页面
    })
  },
  submit(){  //开始订制
   if(this.check()){
     wx.showLoading({title: '加载中...',});
     let _that = this;
     wx.request({   //传后台
       url: app.globalData.url + '/jxb/egoods/egoods/addCompanyInfo',
       method: 'get',
       header: {
         'content-type': 'application/x-www-form-urlencoded'
       },
       data: {
         name: this.data.company,
         contact: this.data.name,
         tel: this.data.tel,
         email: this.data.email,
         count: this.data.num,
         area: this.data.region.join(' '),
         token: wx.getStorageSync('token'),
       },
       success: function (res) {
         console.log(res.data.code)
         if(res.data.code==0){
           wx.setStorageSync('customer', res.data.data.customer);          
           _that.seedemmail(res.data.data.customer)
         }else if(res.data.code==405){
           wx.hideLoading();
           wx.showModal({
             title: '提示',
             content: '帐号异常，已被封禁，请联系客服处理',
             showCancel: false,
             cancelText: '',
             cancelColor: '',
             confirmText: '确定',
             confirmColor: '#2AA7F9',
             success: function (res) {
               if (res.confirm) {
                 wx.switchTab({
                   url: '/pages/index/index',
                 })
               }
             },

           })
         }else{
           wx: wx.showToast({
             title: res.data.msg,
             icon: 'none',
           })
           wx.hideLoading();
         }
         
       },

     })
    
   }
  },
  seedemmail(customer){  //发送邮件
    let that=this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/egoods/egoods/emailPush',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        customer: customer,
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
            that.setData({
              model:true
            })

        }else{
          wx.showToast({
            title: res.data.msg,
          })
        }     
       
      },
      complete(){
        wx.hideLoading()
      }

    })
  },
  suer(){  //跳转添加企业定制
   
    wx.navigateTo({
      url: '/pages/compandz/companydz',
    })
    this.setData({
      model: false
    })
  },
  check(){
    if (this.data.checked == false) {
      wx.showToast({
        title: '请同意并阅读《江小白企业定制特别说明》',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.company == '' || this.data.company.match(/^\s+$/)) {
      wx.showToast({
        title: '请填写企业名称',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.name == '' || this.data.name.match(/^\s+$/)) {
      wx.showToast({
        title: '请填写对接人',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.tel == '') {
      wx.showToast({
        title: '请填写您的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      if (!(/^1[345789]\d{9}$/.test(this.data.tel))) {
        wx.showToast({
          title: '手机格式有误',
          icon: 'none',
          duration: 2000
        });
        return false;
      } else {
      }
    }
    if (this.data.region[0] == '省，市，区') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.num == '') {
      wx.showToast({
        title: '请输入数量',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.num < 200) {
      wx.showToast({
        title: '起订数量为200',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (!(/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(this.data.email))) {
      wx.showToast({
        title: '邮箱格式有误',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true 
  },
  login: function () {   //登录
    wx.showLoading({ title: '登录中...', });
    let that = this;
    wx.login({
      success: res => {
        if (res.code) {  //有code
          app.globalData.code = res.code;
          wx.getSetting({
            success: function (res) {  //已授权
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({  //调用用户信息
                  success: res => { //获取信息   
                    wx.setStorageSync('headurl', res.userInfo.avatarUrl);
                    wx.setStorageSync('username', res.userInfo.nickName);
                    wx.request({   //传后台
                      url: app.globalData.url + '/jxb/login/login/loginAuth',
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        code: app.globalData.code,
                        encryptedData: res.encryptedData,
                        iv: res.iv
                      },
                      success: function (res3) {
                        wx.hideLoading();
                        wx.setStorageSync('token', res3.data.data.token);
                        wx.setStorageSync('uid', res3.data.data.uid);
                        app.globalData.isLogin = true;
                        that.setData({
                          userlogin:true,
                        })
                        wx.showToast({
                          title: '已登录',
                          duration: 2000,
                          icon: 'none',
                        })
                      },

                    })
                  }
                })
              } else {  //未授权

                // that.getUserInfo();
              }
            }

          });// 查看授权

        } else {     //没有code

        }
      }
    })
  },
  getUserInfo(res) {       //授权
    console.log(res.detail.userInfo)
    if (res.detail.userInfo) {
      console.log("点击了同意授权");
      this.login();
    } else {
      console.log("点击了拒绝授权");
    }
  },
  getPhoneNumber(e) {
    var data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      token: wx.getStorageSync('token'),
    }
    let taht = this;
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.request({
        method: "post",
        url: app.globalData.url + '/jxb/login/login/getPhoneNumber',
        data: data,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          wx.setStorageSync('tel', res.data.data.tel)
          taht.setData({
            tellogin: true,
            // tel: res.data.data.tel,
          })
          taht.saveinfo('', res.data.data.tel,'')
          var phoneNum = res.data;
          wx.hideLoading()
          wx.setStorage({
            key: 'tel',
            data: res.data.data.tel,
          })
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function (res) {
          console.log(res)
        }
      })

    }

  },
  saveinfo(remark, tel, avatar) { //保存信息
    wx.request({
      url: app.globalData.url + '/jxb/oneself/updateUserInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        remark: remark,
        tel: tel,
        avatar: avatar,
      },
      success: function (res) {
        // wx.showToast({
        //   title: '修改成功',
        //   icon: 'success',
        //   duration: 2000
        // })
      },

    })
  },

})